package com.asiainfo.fc.util;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class UIHelper {

	private static ResourceBundle rb;
	private static String[] picLiString = { ".jpg", ".png", ".bmp", ".jpeg",
			".gif" };

	public static Color toRGB(String color) {
		if ((color.length() < 6) || (color.length() > 7))
			return Color.black;
		if (color.length() == 7) {
			color = color.substring(1);
		}
		int r = Integer.parseInt(color.substring(0, 2), 16);
		int g = Integer.parseInt(color.substring(2, 4), 16);
		int b = Integer.parseInt(color.substring(4, 6), 16);
		return new Color(r, g, b);
	}

	public static void installUI(String uiName) {
		try {
			UIManager.setLookAndFeel(uiName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	public static boolean contrains(JComboBox items, String str) {
		int len = items.getItemCount();
		for (int i = 0; i < len; i++) {
			if (items.getItemAt(i).toString().equals(str))
				return true;
		}
		return false;
	}

	public static Cursor getCursorByImage(String baseName) {
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image img = getImage(baseName);
		Cursor cu = tk.createCustomCursor(img, new Point(16, 16), "stick");
		return cu;
	}

	public static Image getImage(String basepath) {
		Image img = null;
		try {
			img = ImageIO.read(ClassLoader.getSystemResourceAsStream(basepath));

		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}

	/**
	 * @param basepath
	 * @return
	 */
	public static ImageIcon getImageIco(String basepath) {
		ImageIcon img = new ImageIcon(getImage(basepath));
		return img;
	}

	public static String getResource(String key) {
		if (rb == null) {
			try {
				rb = ResourceBundle.getBundle(
						"com.asiainfo.complier.properties.language",
						Locale.getDefault());
			} catch (Exception ex) {
				throw new NullPointerException(
						"ERROR:CAN NOT FOUND RESOURCE FILE!  "
								+ new File(new StringBuffer()
										.append("language_")
										.append(Locale.getDefault().toString())
										.append(".properties").toString())
										.getAbsolutePath());
			}

		}

		return rb.getString(key);
	}

	public static boolean isImage(File f) {
		for (String str : picLiString) {
			if (f.getName().endsWith(str))
				return true;
		}
		return false;
	}

	public static void updateFont() {
		Font font = new Font("Courier", Font.BOLD, 12);

		UIManager.put("Button.font", font);
		UIManager.put("ToggleButton.font", font);
		UIManager.put("RadioButton.font", font);
		UIManager.put("CheckBox.font", font);
		UIManager.put("ColorChooser.font", font);
		UIManager.put("ToggleButton.font", font);
		UIManager.put("ComboBox.font", font);
		UIManager.put("ComboBoxItem.font", font);
		UIManager.put("InternalFrame.titleFont", font);
		UIManager.put("Label.font", font);
		UIManager.put("List.font", font);
		UIManager.put("MenuBar.font", font);
		UIManager.put("Menu.font", font);
		UIManager.put("MenuItem.font", font);
		UIManager.put("RadioButtonMenuItem.font", font);

		UIManager.put("CheckBoxMenuItem.font", font);
		UIManager.put("PopupMenu.font", font);
		UIManager.put("OptionPane.font", font);
		UIManager.put("Panel.font", font);
		UIManager.put("ProgressBar.font", font);
		UIManager.put("ScrollPane.font", font);
		UIManager.put("Viewport", font);
		UIManager.put("TabbedPane.font", font);
		UIManager.put("TableHeader.font", font);
		UIManager.put("TextField.font", font);
		UIManager.put("PasswordFiled.font", font);
		UIManager.put("TextArea.font", font);
		UIManager.put("TextPane.font", font);
		UIManager.put("EditorPane.font", font);
		UIManager.put("TitledBorder.font", font);
		UIManager.put("ToolBar.font", font);
		UIManager.put("ToolTip.font", font);
		UIManager.put("Tree.font", font);

	}

	public static void setUIFont() {
		Enumeration<Object> keys = UIManager.getDefaults().keys();

		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);

			if (!(value instanceof Font))
				continue;
			Font curFont = (Font) value;
			Font f = new Font(curFont.getFamily(), 0, 12);
			UIManager.put(key, f);
		}
	}

	/**
	 * make center screen
	 */
	public static void centerWindow(Container c) {

		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		int dx = size.width / 2 - c.getWidth() / 2;
		int dy = size.height / 2 - c.getHeight() / 2;
		c.setLocation(dx, dy);
	}

	private UIHelper() {
	}
}